package adc.budget.impl.hb.bo;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import adc.budget.spec.bo.ISource;
import adc.budget.spec.bo.ITransaction;

@Entity
@Table(name="TRANSACTION")
public class HbTransaction extends HbBudgetBo implements ITransaction {

    private static final long serialVersionUID = 1L;

    private Date date;
    private Double amount;
    private String summary;
    private String reference;
    private String notes;
    private ISource source;
    private ISource destination;

    @Override
    @Temporal(TemporalType.DATE)
    @Column(nullable=false)
    public Date getDate() {
        return date;
    }

    @Override
    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    @Column(nullable=false, precision=2)
    public Double getAmount() {
        return amount;
    }

    @Override
    public void setAmount(Double amount) {
        this.amount = amount;
    }

    @Override
    @Column(length=128)
    public String getSummary() {
        return summary;
    }

    @Override
    public void setSummary(String summary) {
        this.summary = summary;
    }

    @Override
    @Column(length=128)
    public String getReference() {
        return reference;
    }

    @Override
    public void setReference(String reference) {
        this.reference = reference;
    }

    @Override
    @Column(length=256)
    public String getNotes() {
        return notes;
    }

    @Override
    public void setNotes(String notes) {
        this.notes = notes;
    }

    @ManyToOne(targetEntity=HbSource.class, optional=false)
    @JoinColumn(nullable=false)
    public ISource getSource() {
        return source;
    }

    public void setSource(ISource source) {
        this.source = source;
    }

    @ManyToOne(targetEntity=HbSource.class, optional=false)
    @JoinColumn(nullable=false)
    public ISource getDestination() {
        return destination;
    }

    public void setDestination(ISource destination) {
        this.destination = destination;
    }

    @Override
    public boolean equals(final Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof HbTransaction)) {
            return false;
        }
        final HbTransaction otherTransact = (HbTransaction) other;
        return equals(this.date, otherTransact.date)
                && equals(this.summary, otherTransact.summary)
                && equals(this.source, otherTransact.source)
                && equals(this.destination, otherTransact.destination);
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(date).append(summary).append(source).append(destination).toHashCode();
    }

    @Override
    protected void appendPropsToString(final ToStringBuilder builder) {
        super.appendPropsToString(builder);
        builder.append("date", date).append("summary").append("amount", amount)
                .append("source", source).append("destination", destination);
    }
}
